package adb.group.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.Drug;
import beans.Operation;
import beans.OutPatient;

/**
 * Servlet implementation class LoadAddTreatmentServlet
 */
public class LoadAddTreatmentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoadAddTreatmentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try{
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Connection connection = (Connection) DriverManager.getConnection(
					"jdbc:oracle:thin:@localhost:1521:ORCL", "dbfp_dba",
					"dbfpdba");
			Statement statement = (Statement) connection.createStatement();
			
			//OutPatient Table
			ResultSet outPatientResult = statement
					.executeQuery("select * from outpatient_t");
			ArrayList<OutPatient> outPatientList= new ArrayList<OutPatient>();
			if (outPatientResult == null) {
				response.sendRedirect("Index.jsp ");
			} else {
				while (outPatientResult.next()) {
					OutPatient outPatient=new OutPatient();
					outPatient.setPatientID(outPatientResult.getString(1));
					outPatient.setVisit_date(outPatientResult.getDate(2));
					outPatientList.add(outPatient);					
				}
				request.getSession().setAttribute("outPatientList", outPatientList);
			}
			
			//Drugs
			ResultSet drugResult= statement.executeQuery("select * from drug_t");
			ArrayList<Drug> drugList=new ArrayList<Drug>();
			while(drugResult.next()){
				Drug drug=new Drug();
				drug.setDrugID(drugResult.getString(1));
				drug.setDescription(drugResult.getString(3));
				drugList.add(drug);
			}
			request.getSession().setAttribute("drugList", drugList);
			
			//Operation
			ResultSet opResult = statement.executeQuery("select * from operation_lab_t");
			ArrayList<Operation> operationList= new ArrayList<Operation>();
			while(opResult.next()){
				Operation operation= new Operation();
				operation.setOperationID(opResult.getString(1));
				operation.setDescription(opResult.getString(2));
				operationList.add(operation);
			}
			request.getSession().setAttribute("operationList", operationList);
			
			//Forward
			response.sendRedirect("/project/resources/physician/addTreatment.jsp");
		}catch(Exception e){
			System.out.print(e);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
